package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.simon.annual.constans.CapitalstructurePref;
import com.simon.annual.constans.CapitalstructureType;
import com.simon.annual.entity.ReportCapitalstructure;
import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.entity.ReportOrdinaryshareholder;
import com.simon.annual.repositoy.IReportCapitalstructureRepositoy;
import com.simon.annual.repositoy.IReportOrdinaryshareholderRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;
/**
 * 第六节股本变动及股东情况
 * @author simon
 * */
@Service
public class EquityShareholderInfoImpl{
	
	@Resource
	private ICompanyPublicInfo companyPublicInfo;
	@Resource
	private IReportOrdinaryshareholderRepositoy reportOrdinaryshareholderRepositoy;
	@Resource
	private IReportCapitalstructureRepositoy reportCapitalstructureRepositoy;

	public ReportNeeq analysis(Element e, ReportNeeq reportNeeq, int companyPtrId) {
		//普通股股本结构
		Element  capitalstructure = companyPublicInfo.findNextTag("table", e);
		Elements reportCapitalstructureTr = capitalstructure.select("table tr");
		List<ReportCapitalstructure> reportCapitalstructureList = new ArrayList<ReportCapitalstructure>();
		//普通股股本结构
		CapitalstructureType[] values =CapitalstructureType.values();
		for (int i = 0; i < values.length; i++) {
			Elements reportCapitalstructureTd = reportCapitalstructureTr.get(i+2).children();
			ReportCapitalstructure reportCapitalstructure= new ReportCapitalstructure();
			int index =-1;
			if(i==0||i==4){index=0;}
			reportCapitalstructure.setType(values[i].getCode());
			reportCapitalstructure.setBgnShares(reportCapitalstructureTd.get(2+index).text());
			reportCapitalstructure.setEndShares(reportCapitalstructureTd.get(5+index).text());
			reportCapitalstructure.setProjectScopeId(companyPtrId);
			reportCapitalstructureList.add(reportCapitalstructure);
		}
		//总股本
		Elements tr2 = reportCapitalstructureTr.get(10).children();
		ReportCapitalstructure reportTr2= new ReportCapitalstructure();
//		Elements reportCapitalstructureTd2 = tr2.select("td");
		reportTr2.setType("Capitalization");
		reportTr2.setBgnShares(tr2.get(1).text());
		reportTr2.setEndShares(tr2.get(4).text());
		reportTr2.setProjectScopeId(companyPtrId);
		reportCapitalstructureList.add(reportTr2);
		
		reportNeeq.setOrdinaryShareholdersNo(capitalstructure.select("table tr > *").last().text());
		//普通股前十名股东情况
		Element ordinaryShareholder = companyPublicInfo.findNextTag("table", capitalstructure);
		Elements ordinarytr = ordinaryShareholder.select("table tr");
		List<ReportOrdinaryshareholder> reportOrdinaryshareholderList = new ArrayList<ReportOrdinaryshareholder>();
		for (int i = 1; i < ordinarytr.size(); i++) {
			Elements td = ordinarytr.get(i).children();
			if(td.size()==ordinarytr.get(i-1).children().size()){
				ReportOrdinaryshareholder reportOrdinaryshareholder = new ReportOrdinaryshareholder();
				reportOrdinaryshareholder.setRank(td.get(0).text());
				reportOrdinaryshareholder.setName(td.get(1).text());
				reportOrdinaryshareholder.setBgnShares(td.get(2).text());
				reportOrdinaryshareholder.setEndShares(td.get(3).text());
				reportOrdinaryshareholder.setEndLimitedShares(td.get(4).text());
				reportOrdinaryshareholder.setEndUnlimitedShares(td.get(5).text());
				reportOrdinaryshareholder.setProjectScopeId(companyPtrId);
				reportOrdinaryshareholderList.add(reportOrdinaryshareholder);
			}else{
				break;
			}
		}
		reportOrdinaryshareholderRepositoy.save(reportOrdinaryshareholderList);
		//前十名股东间相互关系说明
		Element relationship = ordinaryShareholder;
		if(ordinaryShareholder.nextElementSibling().tagName().equals("table")){
			relationship = companyPublicInfo.findNextTag("table", ordinaryShareholder);
			reportNeeq.setTopShareholderRelationship(relationship.text());
		}else{
			reportNeeq.setTopShareholderRelationship(ordinarytr.last().text());
		}
		//优先股股本基本情况
		Elements pref  = relationship.parent().getElementsMatchingOwnText("\u4F18\u5148\u80A1\u80A1\u672C\u57FA\u672C\u60C5\u51B5");//优先股股本基本情况
//		Element pref = companyPublicInfo.findNextTag("table", relationship);
		Elements prefTr = pref.get(0).nextElementSibling().select("table tr");
		if(prefTr.size()>1){
			CapitalstructurePref[] prefs =CapitalstructurePref.values();
			for (int i = 0; i < prefs.length; i++) {
				Elements prefTds = prefTr.get(i+1).children();
				ReportCapitalstructure reportCapitalstructure= new ReportCapitalstructure();
				reportCapitalstructure.setType(values[i].getCode());
				reportCapitalstructure.setBgnShares(prefTds.get(1).text());
				reportCapitalstructure.setEndShares(prefTds.get(3).text());
				reportCapitalstructure.setProjectScopeId(companyPtrId);
				reportCapitalstructureList.add(reportCapitalstructure);
			}
		}
		reportCapitalstructureRepositoy.save(reportCapitalstructureList);
		//控股股东、实际控制人情况
		Element controlling = companyPublicInfo.findNextTag("table", pref.get(0));
		reportNeeq.setControllingStakeInfo(controlling.text());
		//实际控制人情况
		Element actualController = companyPublicInfo.findNextTag("table", controlling);
		reportNeeq.setControllingStakeInfo(actualController.text());
		return reportNeeq;
	}

}
